<?php
	/* 
		RSS Feed Generator for GCSM
		Copyright © 2007-2010 by Eric Hokanson
		Last changed on $LastChangedDate: 2009-02-23 16:44:26 -0700 (Mon, 23 Feb 2009) $
		Revision $Revision: 34 $

		This program is free software: you can redistribute it and/or modify
		it under the terms of the GNU General Public License as published by
		the Free Software Foundation, either version 3 of the License, or
		(at your option) any later version.
		
		This program is distributed in the hope that it will be useful,
		but WITHOUT ANY WARRANTY; without even the implied warranty of
		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
		GNU General Public License for more details.
		
		You should have received a copy of the GNU General Public License
		along with this program.  If not, see <http://www.gnu.org/licenses/>.
	*/
	require('dbconfig.php');
	require('Zend/Cache.php');

	if (isset($_GET['type']) && $_GET['type'] == 'RSS2.0')
	    $type = 'rss';
	else
	    $type = 'atom';

	// Configure our Zend Framework cache
	$frontendOptions = array('lifetime' => 300);
	$backendOptions = array('cache_dir' => './cache/');
	$cache = Zend_Cache::factory('Output', 'File', $frontendOptions, $backendOptions);

	// Cloned from Google's feed headers
	header("Content-Type: text/xml; charset=UTF-8");
	header("Cache-Control: private");
	
	if(!$cache->start($type)) {
		require('Zend/Feed.php');

		$config = $db->fetchRow("SELECT * FROM config");

		// Union our regular news posts and RSS feeds
		$rs = $db->query("(SELECT id,title,content,NULL as source,UNIX_TIMESTAMP(pubdate) as timestamp,tags,NULL as annotation,'news' as type FROM news WHERE published = 1)
		                   UNION (SELECT id,title,summary as content,source,timestamp,tags,annotation,'reader' as type FROM reader) ORDER BY timestamp DESC LIMIT 20");

		// Build feed array
		$feed = array();
		while ($row = $rs->fetch())
			$feed[] = array("id" => $row['id'], "title" => $row['title'], "source" => $row['source'], "content" => $row['content'], "pubdate" => $row['timestamp'], "annotation" => $row['annotation'], "tags" => $row['tags'], "type" => $row['type']);

		// Make proper Zend_Feed
		$array = array('title' => $config['sitename'], 'link' => $config['siteURL'], 'lastUpdate' => time(), 'charset' => 'utf-8', 'description' => $config['sitedesc'], 'author' => $config['siteauthor'], 'entries' => array());
		$entry = 0;
		foreach($feed as $v) {
			// HTML title's don't appear to be supported
			// HTML is also not allowed in description
			$array['entries'][$entry] = array('title' => html_entity_decode($v['title'], ENT_QUOTES, 'UTF-8'),
											  'link' => rtrim($config['siteURL'], '/') . '/index.php?mod=news&type=' . $v['type'] . '&id=' . $v['id'],
											  'description' => html_entity_decode(strip_tags($v['content']), ENT_QUOTES, 'UTF-8'),
											  'content' => $v['content'],
											  'lastUpdate' => $v['pubdate']);
			if ($v['type'] == 'reader')
				$array['entries'][$entry]['source'] = array('title' => $v['title'], 'url' => $v['source']);
			if (!empty($v['tags'])) {
				$array['entries'][$entry]['category'] = array();
				foreach(explode(',', $v['tags']) as $t)
					$array['entries'][$entry]['category'][] = array('term' => trim($t));
			}
			if ($type == 'rss') // Possible bug in Zend_Feed?
				$array['entries'][$entry]['link'] = htmlspecialchars($array['entries'][$entry]['link']);
			$entry++;
		}

		$feedFromArray = @Zend_Feed::importArray($array, $type);
	
		if ($feedFromArray instanceof Zend_Feed_Abstract)
			print $feedFromArray->saveXML();  // send feed to client
		$cache->end();
	}
?>